package cn.itcast.mq.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/*
 *消费者监听器类
 *@Author gegaojie
 *@Date 2025/1/5 15:47
 */
@Component
@Slf4j
public class ConsumerListener {
    /**
     * 简单工作模型-消费者监听器方法
     * @param msg
     */
    @RabbitListener(queues = "simple.queue")
    public void simple(String msg){
        log.info("[简单工作模型]消费者监听方法，监听到消息{}:", msg);
    }

    /**
     * 工作队列模型-消费者监听器方法
     * @param msg
     */
    @RabbitListener(queues = "work.queue")
    public void workQueue1(String msg) throws InterruptedException {
        log.info("[简单工作模型]消费者监听方法1，监听到消息{}:", msg);
        TimeUnit.MILLISECONDS.sleep(10);//模拟处理耗时10毫秒
    }

    /**
     * 工作队列模型-消费者监听器方法
     * @param msg
     */
    @RabbitListener(queues = "work.queue")
    public void workQueue2(String msg) throws InterruptedException {
        log.info("[简单工作模型]消费者监听方法2，监听到消息{}:", msg);
        TimeUnit.MILLISECONDS.sleep(100);//模拟处理耗时100毫秒
    }

    /**
     * 广播模型-消费者监听器方法1
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue1")
    public void fanoutQueue1(String msg) throws InterruptedException {
        log.info("[广播模型]消费者监听方法1，监听到消息{}:", msg);
    }

    /**
     * 广播模型-消费者监听器方法2
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue2")
    public void fanoutQueue2(String msg) throws InterruptedException {
        log.info("[广播模型]消费者监听方法2，监听到消息{}:", msg);
    }

    /**
     * 路由模型-消费者监听器方法1
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue1"),
                                             exchange = @Exchange(name = "direct.exchange",type = ExchangeTypes.DIRECT),
                                             key ={"blue","red"}))
    public void dricctQueue1(String msg) throws InterruptedException {
        log.info("[路由模型]消费者监听方法1，监听到消息{}:", msg);
    }

    /**
     * 路由模型-消费者监听器方法2
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "direct.exchange",type = ExchangeTypes.DIRECT),
            key ={"yellow","red"}))
    public void dricctQueue2(String msg) throws InterruptedException {
        log.info("[路由模型]消费者监听方法2，监听到消息{}:", msg);
    }

    /**
     * 主题模型-消费者监听器方法1
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "topic.exchange",type = ExchangeTypes.TOPIC),
            key ={"china.#"}))
    public void topicQueue1(String msg) throws InterruptedException {
        log.info("[主题模型]消费者监听方法1，监听到消息{}:", msg);
    }

    /**
     * 主题模型-消费者监听器方法2
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "topic.exchange",type = ExchangeTypes.TOPIC),
            key ={"*.news"}))
    public void topicQueue2(String msg) throws InterruptedException {
        log.info("[主题模型]消费者监听方法2，监听到消息{}:", msg);
    }
}
